import QtQuick 2.0
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1
import "../FKWidget/"

FKWidget{
    id:roomListWidget
    relativeWidth: 0.9
    relativeHeight: 0.9
    onShow:{
//        refreshRequested()
        refresher.start()
    }
    onHide: refresher.stop()

    Timer{
        id:refresher
        interval:10000
        repeat:true
        running: false
        onTriggered: roomListWidget.refreshRequested()
    }

    signal refreshRequested
    signal canceled
    signal enterRoomRequested(var roomID)
    signal createRoomRequested(var roomType, var roomName, var description, var password)

    onCanceled: hide()

    FKViewport{
        Rectangle{
            id:lst
            anchors{
                left:parent.left
                right:parent.right
                top: parent.top
                bottom:buttons.top
                margins: 10
            }
            ListView{
                id:roomListView
                anchors.fill: parent
                model:roomListModel
                delegate: rowDelegate
                highlight: Rectangle{color:"blue";width:lst.width}
            }
        }
        Row{
            id:buttons
            property int buttonWidth:(width-spacing*3)/4
            spacing: 10
            anchors{
                bottom:parent.bottom
                left:parent.left
                right:parent.right
                margins: 10
            }
            //height:100
            Button{
                text:qsTr("Create room")
                width:parent.buttonWidth
                onClicked: roomCreation.show()
            }
            Button{
                text:qsTr("Enter room")
                width:parent.buttonWidth
                onClicked: roomListWidget.enterRoomRequested(roomListView.currentItem.room)
            }
            Button{
                text:qsTr("Refresh list")
                width:parent.buttonWidth
                onClicked: roomListWidget.refreshRequested()
            }
            Button{
                text:qsTr("Back")
                width:parent.buttonWidth
                onClicked: roomListWidget.canceled()
            }
        }
        Component{
            id:rowDelegate
            Item{
                id: rowItem
                width: lst.width
                height: r.height
                property int room: objectID
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        rowItem.ListView.view.currentIndex=index
                    }
                }
                Row{
                    id:r
                    anchors{left: parent.left; top:parent.top;}
                    height:rn.height
                    Text{
                        text: objectID
                        width: rowItem.width*0.05
                        font.pointSize: 13
                    }
                    Text{
                        text: roomType
                        width: rowItem.width*0.2
                        font.pointSize: 13
                    }
                    Text{
                        id:rn
                        text: nameRole
                        width: rowItem.width*0.25
                        font.pointSize: 13
                    }
                    Text{
//                        text: roomDescription
                        width: rowItem.width*0.4
                        font.pointSize: 13
                    }
                    Text{
//                        text: roomUsedCapacity
                        width: rowItem.width*0.05
                        font.pointSize: 13
                    }
                    Text{
//                        text: roomCapacity
                        width: rowItem.width*0.5
                        font.pointSize: 13
                    }
                }
            }
        }
        FKWidget{
            id:roomCreation
            stacker: roomListWidget
            relativeWidth: 0.3
            relativeHeight: 0.4
            FKViewport{
                Column{
                    anchors.centerIn: parent
                    width:parent.width-20
                    spacing: 5
                   // anchors.margins: 10
                    Text{
                        text:qsTr("Room type:")
                    }
                    ComboBox{
                        id:roomType
                        width:parent.width
                        model: fkvi.avaliableRooms
                    }
                    Text{
                        text:qsTr("Room name:")
                    }
                    TextField{
                        id:roomName
                        text:"Test room"
                        width:parent.width
                    }
                    Text{
                        text:qsTr("Description (optional):")
                    }
                    TextField{
                        id:roomDescription
                        width:parent.width
                    }
                    Text{
                        text:qsTr("Password (optional):")
                    }
                    TextField{
                        id:roomPassword
                        width:parent.width
                    }
                    Button{
                        text:qsTr("Create")
                        width:parent.width
                        onClicked:{
                            roomCreation.hide()
                            roomListWidget.createRoomRequested(roomType.currentText,roomName.text,roomDescription.text,roomPassword.text)
                            roomListWidget.refreshRequested()
                        }
                    }
                    Button{
                        text:qsTr("Cancel")
                        width:parent.width
                        onClicked:roomCreation.hide()
                    }
                }
            }
        }
    }
}
